/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegistroFrame.java
 *
 * Created on 02-ene-2012, 16:09:30
 */
package occho.view;


import java.sql.SQLException;
import javax.swing.JOptionPane;
import occho.model.FachadaRegistro;
import occho.model.Usuario;
import occho.view.resources.Messages;

/**
 *
 * @author Jorge
 */
public class RegistroFrame extends javax.swing.JInternalFrame implements Internacionalizable {
    private OcchoView view;
    /** Creates new form RegistroFrame
     * @param view 
     */
    public RegistroFrame(OcchoView view) {
        initComponents();
        this.view = view;
        setLenguaje();
        fachada = new FachadaRegistro();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbNombre = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lbApellidos = new javax.swing.JLabel();
        txtApellidos = new javax.swing.JTextField();
        lbEmail = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        lbPassword1 = new javax.swing.JLabel();
        txtPassword1 = new javax.swing.JPasswordField();
        lbPassword2 = new javax.swing.JLabel();
        txtPassword2 = new javax.swing.JPasswordField();
        btnRegistrar = new javax.swing.JButton();

        setClosable(true);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(occho.OcchoApp.class).getContext().getResourceMap(RegistroFrame.class);
        lbNombre.setText(resourceMap.getString("lbNombre.text")); // NOI18N
        lbNombre.setName("lbNombre"); // NOI18N

        txtNombre.setText(resourceMap.getString("txtNombre.text")); // NOI18N
        txtNombre.setName("txtNombre"); // NOI18N

        lbApellidos.setText(resourceMap.getString("lbApellidos.text")); // NOI18N
        lbApellidos.setName("lbApellidos"); // NOI18N

        txtApellidos.setText(resourceMap.getString("txtApellidos.text")); // NOI18N
        txtApellidos.setName("txtApellidos"); // NOI18N

        lbEmail.setText(resourceMap.getString("lbEmail.text")); // NOI18N
        lbEmail.setName("lbEmail"); // NOI18N

        txtEmail.setText(resourceMap.getString("txtEmail.text")); // NOI18N
        txtEmail.setName("txtEmail"); // NOI18N

        lbPassword1.setText(resourceMap.getString("lbPassword1.text")); // NOI18N
        lbPassword1.setName("lbPassword1"); // NOI18N

        txtPassword1.setText(resourceMap.getString("txtPassword1.text")); // NOI18N
        txtPassword1.setName("txtPassword1"); // NOI18N

        lbPassword2.setText(resourceMap.getString("lbPassword2.text")); // NOI18N
        lbPassword2.setName("lbPassword2"); // NOI18N

        txtPassword2.setText(resourceMap.getString("txtPassword2.text")); // NOI18N
        txtPassword2.setName("txtPassword2"); // NOI18N

        btnRegistrar.setText(resourceMap.getString("btnRegistrar.text")); // NOI18N
        btnRegistrar.setName("btnRegistrar"); // NOI18N
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(lbNombre)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 84, Short.MAX_VALUE)
                        .add(txtNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(lbApellidos)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 75, Short.MAX_VALUE)
                        .add(txtApellidos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(lbEmail)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 100, Short.MAX_VALUE)
                        .add(txtEmail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(lbPassword1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 63, Short.MAX_VALUE)
                        .add(txtPassword1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(lbPassword2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 22, Short.MAX_VALUE)
                        .add(txtPassword2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, btnRegistrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 115, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbNombre)
                    .add(txtNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbApellidos)
                    .add(txtApellidos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbEmail)
                    .add(txtEmail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbPassword1)
                    .add(txtPassword1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbPassword2)
                    .add(txtPassword2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(btnRegistrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
    if (validarRegistro()) {
            try {
                if (fachada.registrarUsuario(txtNombre.getText(), txtApellidos.getText(), 
                        txtEmail.getText(), new String(txtPassword1.getPassword()), Usuario.ALUMNO)) {
                    JOptionPane.showMessageDialog(null, "Se ha registrado con éxito");
                    this.dispose();
                }
            } catch (ClassNotFoundException ex) {
                System.out.println("Class_Not_Found_Exception: " + ex.getMessage());
            } catch (SQLException ex) {
                System.out.println("SQL_Exception: " + ex.getMessage());
            }
    }
    else{
        JOptionPane.showMessageDialog(null,"No se han rellenado todos los campos","Error",JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_btnRegistrarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JLabel lbApellidos;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JLabel lbNombre;
    private javax.swing.JLabel lbPassword1;
    private javax.swing.JLabel lbPassword2;
    private javax.swing.JTextField txtApellidos;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JPasswordField txtPassword1;
    private javax.swing.JPasswordField txtPassword2;
    // End of variables declaration//GEN-END:variables

    // Atributo fachada
    private FachadaRegistro fachada;
    
    private boolean validarRegistro() {
        String pass1 = new String(txtPassword1.getPassword());
        String pass2 = new String(txtPassword2.getPassword());
        
        if (txtNombre.getText().isEmpty() || txtApellidos.getText().isEmpty() ||
                txtEmail.getText().isEmpty() || pass1.isEmpty() || pass2.isEmpty() ||
                !pass1.equals(pass2))
            return false;
        return true;
    }

    /**
     * Establece lenguaje
     */
    public void setLenguaje() {
        lbNombre.setText(Messages.getString("lbNombre.text", Messages.BUNDLE_NAME_REGISTRO));
        lbApellidos.setText(Messages.getString("lbApellidos.text", Messages.BUNDLE_NAME_REGISTRO));
        lbEmail.setText(Messages.getString("lbEmail.text", Messages.BUNDLE_NAME_REGISTRO));
        lbPassword1.setText(Messages.getString("lbPassword1.text", Messages.BUNDLE_NAME_REGISTRO));
        lbPassword2.setText(Messages.getString("lbPassword2.text", Messages.BUNDLE_NAME_REGISTRO));
        btnRegistrar.setText(Messages.getString("btnRegistrar.text", Messages.BUNDLE_NAME_REGISTRO));
        setTitle(Messages.getString("Form.title", Messages.BUNDLE_NAME_REGISTRO));      
    }
}
